package com.cn.web.org.controller;

import com.cn.annoation.Logs;
import com.cn.msg.ResultMsg;
import com.cn.page.PageVo;
import com.cn.util.BaseController;
import com.cn.util.ExcelExportUtil;
import com.cn.web.org.service.OrgService;
import com.cn.web.org.vo.OrgEditDTO;
import com.cn.web.org.vo.OrgSearch;
import com.cn.web.org.vo.OrgVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * description: OrgController <br>
 * date: 21.12.16 14:59 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping(value = "admin/org")
@Api(tags = "用户中心--组织机构")
public class OrgController extends BaseController {

    @Autowired
    private OrgService orgService;

    
    @Logs(value = "编辑", desc = "组织机构")
    @ApiOperation(value = "编辑")
    @PostMapping(value = "edit")
    public ResultMsg edit(
            @RequestBody @Validated OrgEditDTO org
    ) {
        this.orgService.add(org);
        return ResultMsg.builder();
    }

    
    @ApiOperation(value = "查看")
    @GetMapping(value = "find")
    public ResultMsg<OrgEditDTO> find(
            @ApiParam(name = "id") @RequestParam String id,
            @ApiParam(name = "flag", value = "true-查看原始数据 false-查看转义数据") @RequestParam Boolean flag
    ) {
        OrgEditDTO data = null;
        data = this.orgService.find(id, flag);
        return ResultMsg.builder().setData(data);
    }

    
    @Logs(value = "删除", desc = "组织机构")
    @ApiOperation(value = "删除")
    @GetMapping(value = "del")
    public ResultMsg del(
            @ApiParam(name = "id") @RequestParam String id
    ) {
        this.orgService.del(id);
        return ResultMsg.builder();
    }


    
    @ApiOperation(value = "分页")
    @PostMapping(value = "page")
    public ResultMsg<PageInfo<OrgVO>> page(
            @RequestBody PageVo<OrgSearch> pageVo
    ) {
        PageInfo<OrgVO> page = null;
        page = this.orgService.page(pageVo);
        return ResultMsg.builder().setData(page);
    }


    
    @ApiOperation(value = "单位树")
    @GetMapping(value = "listTree")
    public ResultMsg listTree(
            @ApiParam(name = "parentNo") @RequestParam(required = false) String parentNo
    ) {
        OrgSearch search = new OrgSearch();
        if (StringUtils.isNotEmpty(parentNo)) {
            search.setParentNo(parentNo);
        }
        return ResultMsg.builder().setData(this.orgService.list(search));
    }

    
    @ApiOperation(value = "获取部门树及部门下的人员，返回双数组(仿照钉钉用户面板形式)-V2")
    @GetMapping(value = "listUnitTreeV2")
    public ResultMsg listUnitTreeV2(
    ) {
        return ResultMsg.builder().setData(this.orgService.packProcessUnitAndUserV2());
    }

    
    @ApiOperation(value = "获取部门树及部门下的人员，返回双数组(仿照钉钉用户面板形式)")
    @GetMapping(value = "listUnitTree")
    public ResultMsg listUnitTree(
    ) {
        return ResultMsg.builder().setData(this.orgService.packProcessUnitAndUser(1));
    }

    
    @ApiOperation(value = "导出")
    @PostMapping(value = "export")
    public void export(
            @RequestBody PageVo<OrgSearch> pageVo
    ) {
        var page = this.orgService.page(pageVo);
        if (page.getList() != null) {
            var list = page.getList();

            ExcelExportUtil.export1(response, "机构", "机构", OrgVO.class, list);
        }
    }
}
